<template>
  <el-container class='home-container'>
    <el-header>
      <div>
        <span>电商后台管理系统</span>
      </div>
      <el-button type='info' @click="exit">
        退出
      </el-button>
    </el-header>

    <el-container>
      <el-aside :width="iscollopse ? '64px':'200px' ">
        <div class = "toggle-button" @click = "toggle">|||</div>
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#409EFF" unique-opened="true" :collapse = "iscollopse" router
        :default-active="activePath">
          <!-- 一级菜单 -->
          <el-submenu  v-for = "item in menulist" :key = "item.id" :index = "item.id">
            <!-- 一级菜单模版区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i class = 'el-icon-menu'></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index = "'/'+subitem.path" v-for = "subitem in item.children" :key = 'subitem.id' @click="saveNavState('/'+subitem.path)">
              <template slot="title">
                <!-- 图标 -->
                <i class = 'el-icon-menu'></i>
                <!-- 文本 -->
                <span>{{subitem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  created () {
    this.getMenuList()
  },

  data () {
    return {
      menulist: [],
      iscollopse: false,
      activePath: ''
    }
  },
  methods: {
    exit () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList () {
      // 获取菜单数据
      const { data: res } = await this.$http.get('menus')
      // res中是数据
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error('res.meta.msg')
      }
      this.menulist = res.data
    },
    toggle () {
      // 点击按钮实现菜单的折叠和展开
      this.iscollopse = !this.iscollopse
    },
    saveNavState (activePath) {
      // 保存链接的激活状态

      this.activePath = activePath
    }

  }
}
</script>
<style scoped>
    .el-container {
        height: 100%;
    }

    .el-header {
        display: flex;
        justify-content: space-between;
        background-color: #373D41;
        padding-left: 0;
        align-items: center;
        color: white;
        font-size: 20px;
    }

    .el-header div {
        display: flex;
        align-items: center;
    }

    .el-header div span {
        margin-left: 15px;
    }

    .el-aside {
        background-color: #333744;
    }

    .el-main {
        background-color: #eaedf1;
    }
    .toggle-button{
      background-color: #4A5064;
      font-size: 10px;
      line-height: 24px;
      color: #fff;
      text-align: center;
      letter-spacing: 0.2em;
      cursor: pointer;
    }
</style>
